﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.DetailedDataUtils.CUiDtuBaseEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.DetailedDataUtils;

internal abstract class CUiDtuBaseEnabler : ITxCommandEnabler
{
  internal TxTypeFilter EnabledObjectsFilter
  {
    get
    {
      TxTypeFilter componentFilter = this.ComponentFilter;
      componentFilter.AddIncludedType(typeof (ITxCompound));
      componentFilter.AddIncludedType(typeof (TxPhysicalRoot));
      componentFilter.AddIncludedType(typeof (ITxLogicalCollection));
      return componentFilter;
    }
  }

  internal TxTypeFilter ComponentFilter
  {
    get
    {
      TxTypeFilter componentFilter = new TxTypeFilter();
      componentFilter.AddIncludedType(typeof (ITxComponent));
      componentFilter.AddExcludedType(typeof (TxHuman));
      componentFilter.AddExcludedType(typeof (TxPartAppearance));
      componentFilter.AddExcludedType(typeof (ITxDetailedRepresentation));
      componentFilter.AddExcludedType(typeof (TxCable));
      return componentFilter;
    }
  }

  private bool CheckEnable()
  {
    bool flag = false;
    TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) this.EnabledObjectsFilter);
    if (((Collection<ITxObject>) filteredItems).Count > 0)
      flag = true;
    foreach (ITxObject itxObject in (Collection<ITxObject>) filteredItems)
    {
      if (itxObject is ITxComponent itxComponent && itxComponent.IsOpenForModeling)
      {
        flag = false;
      }
      else
      {
        flag = true;
        break;
      }
    }
    if (flag)
    {
      foreach (ITxObject itxObject in (Collection<ITxObject>) filteredItems)
      {
        if (!this.IsSuperComponent(itxObject as ITxComponent) && !this.AllowedComponentRepresentation(itxObject as ITxComponent))
        {
          flag = false;
        }
        else
        {
          flag = true;
          break;
        }
      }
    }
    if (flag)
    {
      foreach (ITxObject itxObject in (Collection<ITxObject>) filteredItems)
      {
        TxCompoundResource compoundResource = itxObject as TxCompoundResource;
        if (TxCompoundResource.op_Inequality(compoundResource, (ITxObject) null) && compoundResource.Count == 0)
        {
          flag = false;
          break;
        }
        TxCompoundPart txCompoundPart = itxObject as TxCompoundPart;
        if (TxCompoundPart.op_Inequality(txCompoundPart, (ITxObject) null) && txCompoundPart.Count == 0)
        {
          flag = false;
          break;
        }
        if (itxObject is TxLogicalCollection logicalCollection && logicalCollection.Count == 0)
        {
          flag = false;
          break;
        }
        if (itxObject is TxLogicalGroup txLogicalGroup && txLogicalGroup.Count == 0)
        {
          flag = false;
          break;
        }
      }
    }
    return flag;
  }

  private bool IsSuperComponent(ITxComponent comp)
  {
    bool flag = false;
    if (comp != null)
      flag = comp.IsSuperComponent;
    return flag;
  }

  internal abstract bool AllowedComponentRepresentation(ITxComponent component);

  private ITxComponent GetParentComponent(ITxObject obj)
  {
    ITxObject collection = (ITxObject) obj.Collection;
    while (true)
    {
      switch (collection)
      {
        case null:
        case ITxComponent _:
          goto label_3;
        default:
          collection = (ITxObject) collection.Collection;
          continue;
      }
    }
label_3:
    return collection as ITxComponent;
  }

  public bool Enable => this.CheckEnable();
}
